// components/bucket/bucket.js
const app = getApp()
import {
  list as productList
} from '../../api/product'
import {
  add,
  buyNow
} from '../../api/cart'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    productsType: {
      type: Boolean,
      value: false,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {

    currentNum: 0,
    productShow: false,
    products: [],
    productIndex:0
  },
  lifetimes: {
    attached: function () {
      this.loadProduct()
      // 在组件实例进入页面节点树时执行
      //可视窗口x,y坐标
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    numReduce(e) {
      if (this.data.products[e.target.dataset.index].quantity > 0) {
        this.data.products[e.target.dataset.index].quantity -= 1
      } else if (this.data.products[e.target.dataset.index].quantity == 0 || this.data.products[e.target.dataset.index].quantity < 0) {
        this.data.products[e.target.dataset.index].quantity = 0
        wx.showToast({
          title: '商品数量不能再少了呦！',
          icon: 'none',
          duration: 1500
        })
      }
      this.setData({
        products: this.data.products
      })
    },


    toBuy(e) {
     
      if(this.data.productsType == false ){
        if (this.data.products[this.data.productIndex].quantity==0) {
          wx.showToast({
            title: '空桶数量不能小于0',
            icon: 'none',
            duration: 1500
          })
          return
        }
        buyNow({
          uuid: app.globalData.uuid,
          quantity: this.data.products[this.data.productIndex].quantity,
          productId: this.data.products[this.data.productIndex].id
        }).then(res => {
          this.data.productShow=false
          wx.navigateTo({
            url: '/pages/goods/submit/index',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
          this.setData({
            productShow:this.data.productShow
          })
        })
      }else if(this.data.productsType == true){
        console.log('添加商品')
        this.triggerEvent('productsAdd',this.data.products[this.data.productIndex])
      }
    },
    numAdd(e) {
      console.log(e)
      this.data.products[e.target.dataset.index].quantity += 1
      this.setData({
        products: this.data.products
      })
    },
    bindblur(e) {
      if (e.detail.value >= 0) {
        this.data.products[e.target.dataset.index].quantity = Number(e.detail.value)
      } else {
        this.data.products[e.target.dataset.index].quantity = 0
        wx.showToast({
          title: '商品数量不能再少了呦！',
          icon: 'none',
          duration: 1500
        })
      }
      this.setData({
        products: this.data.products
      })
    },
    loadProduct() {
      productList({
        mchId:app.globalData.mchId,
        subType: 3,
        type: 1
      }).then(res => {
        this.data.products = res.data.data
        res.data.data.forEach(item => {
          item.quantity = 2
        })

        this.setData({
          products: this.data.products
        })
      })
    },
    isShowClick() {
      console.log('2222')
      this.data.productShow = true
      this.setData({
        productShow: this.data.productShow
      })
    },
    closeProduct() {
      this.data.productShow = false
      this.setData({
        productShow: this.data.productShow
      })
    },
    productClick(e){
      console.log(e)
      this.data.productIndex= e.currentTarget.dataset.index
      this.data.products[this.data.productIndex].quantity = 2;
      this.setData({
        products: this.data.products,
        productIndex:this.data.productIndex
      })
    }
  }
})